package servlet;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.NewsBean;
import bean.NotificationBean;
import dao.NewsDao;
import dao.NotificationDao;
import net.sf.json.JSONArray;

public class GetNewsServlet extends HttpServlet{
		private static final long serialVersionUID = 1L;
	       
	    /**
	     * @see HttpServlet#HttpServlet()
	     */
	    public GetNewsServlet() {
	        super();
	        // TODO Auto-generated constructor stub
	    }

		/**
		 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
		 */
		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			// TODO Auto-generated method stub
			doPost(request, response);
		}

		/**
		 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
		 */
		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			// TODO Auto-generated method stub

			List<NewsBean> list = null;
			try {
				list = (new NewsDao()).getNews();
			} catch (ClassNotFoundException e) {
				// TODO 鑷姩鐢熸垚鐨� catch 鍧�
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO 鑷姩鐢熸垚鐨� catch 鍧�
				e.printStackTrace();
			}
			JSONArray repJson = JSONArray.fromObject(list);
			OutputStream os = response.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			bw.write(repJson.toString());
			bw.flush();
			bw.close();
		}

}
